package com.alibaba.citrus.cr.unified.inventory.functions.allocateOrder;

import java.util.Date;
import java.util.List;

import com.alibaba.citrus.cr.common.util.JsonUtil;
import com.alibaba.citrus.cr.unified.inventory.functions.allocateOrderDetail.AllocateOrderDetailEntity;
import com.alibaba.fastjson.JSONObject;

import com.epoch.app.crunifiedinventory.dto.request.AllocateOrderSalesOrderUpdateRequest;
import com.epoch.app.crunifiedinventory.dto.request.AllocateOrderSalesOrderUpdateScItemDetail;
import com.epoch.app.crunifiedinventory.dto.request.AllocateOrderSyncRequest;
import com.epoch.app.crunifiedinventory.model.dto.AllocateOrderCreateDTO;
import com.epoch.app.crunifiedinventory.model.dto.AllocateOrderCreateRequest;
import com.epoch.app.crunifiedinventory.model.dto.AllocateOrderScItemDetailDTO;
import com.epoch.app.crunifiedinventory.sdo.AllocateOrderDetailSDO;
import com.epoch.app.crunifiedinventory.sdo.AllocateOrderSDO;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;

public class AllocateOrderFactory {
    public AllocateOrderEntity createA() {
        return new AllocateOrderEntity();
    }

    public static AllocateOrderEntity salesOrderUpdate(AllocateOrderSalesOrderUpdateRequest r){
        AllocateOrderEntity entity = of(r);
        return entity;
    }
    public static AllocateOrderEntity of(AllocateOrderSalesOrderUpdateRequest r){
        AllocateOrderSDO allocateOrderSDO = convert(r);
        if (allocateOrderSDO == null)return null;
        AllocateOrderEntity entity = new AllocateOrderEntity();
        entity.setAllocateOrderSDO(allocateOrderSDO);
        if (CollectionUtils.isNotEmpty(r.getScItemDetails())){
            List<AllocateOrderDetailEntity> detailEntities = Lists.newArrayList();
            for (AllocateOrderSalesOrderUpdateScItemDetail d: r.getScItemDetails()){
                AllocateOrderDetailSDO sdo = convert(d);
                if (sdo != null){
                    AllocateOrderDetailEntity detailEntity = new AllocateOrderDetailEntity();
                    detailEntity.setAllocateOrderDetailSDO(sdo);
                    detailEntities.add(detailEntity);
                }
                entity.setAllocateOrderDetailEntities(detailEntities);
            }
        }
        return entity;
    }

    public static AllocateOrderSDO convert(AllocateOrderSalesOrderUpdateRequest request){
        AllocateOrderSDO sdo = new AllocateOrderSDO();
        sdo = JsonUtil.convert(request,AllocateOrderSDO.class);
        return sdo;
    }
    public static AllocateOrderDetailSDO convert(AllocateOrderSalesOrderUpdateScItemDetail detail){
        AllocateOrderDetailSDO sdo = JsonUtil.convert(detail,AllocateOrderDetailSDO.class);
        return sdo;
    }

    /**
     * 构建一个调拨单entity
     * @param allocateOrderCreateRequest
     * @return
     */
    public static AllocateOrderEntity createAllocateOrder(AllocateOrderCreateRequest allocateOrderCreateRequest){
//        AllocateOrderSDO allocateOrderSDO = convert(allocateOrderCreateRequest);
        AllocateOrderEntity allocateOrderEntity = AllocateOrderEntity.of(allocateOrderCreateRequest);
        return allocateOrderEntity;
    }
//    public static AllocateOrderSDO convert(AllocateOrderCreateRequest allocateOrderCreateRequest){
//        AllocateOrderSDO allocateOrderSDO = new AllocateOrderSDO();
////        allocateOrderSDO.
//        return JSONObject.parseObject(JSONObject.toJSONString(allocateOrderCreateRequest.getAllocateOrderCreateDTO()),
//                AllocateOrderSDO.class);
//    }


    public static AllocateOrderEntity createAllocateOrder(AllocateOrderSyncRequest allocateOrderSyncRequest){
        AllocateOrderEntity allocateOrderEntity = AllocateOrderEntity.of(allocateOrderSyncRequest);

        return allocateOrderEntity;
    }

//    public static AllocateOrderEntity sync()

    public static void main(String[] args) {
        AllocateOrderCreateDTO dto = new AllocateOrderCreateDTO();
        dto.setAllocateApplyOrderId(11);
        dto.setAllocateApplyOrderCode("调拨单预报单号");
        List<AllocateOrderScItemDetailDTO> list = Lists.newArrayList();
        AllocateOrderScItemDetailDTO d = new AllocateOrderScItemDetailDTO();
        d.setAllocateApplyOrderLineNo("调拨预报单行号");
        d.setUnit("单位");
        d.setClosed(false);
        d.setStatus(1);
        d.setScItemName("货品");
        d.setUnitPrice("单价");
        d.setSubTotalAmount("12");
//        d.setStatus();
        d.setShipQuantity("12");
        d.setSettlementAmount("12");
        d.setScItemCode("12");
        d.setReceiveWarehouseInventory("112");
        d.setReceiveQuantity("12");
        d.setQuantity("12");
        d.setOutAllocateOrderLineNo("12");
        d.setLineNo("12");
//        d.setConfirmedQuantity("12");
//        d.setClosed(/);
        d.setDiscount("12");
        d.setAllocateOrderId("12");
        list.add(d);
        dto.setAllocateOrderScItemDetails(list);
        dto.setType(1);
        dto.setCode("STO111111");
        dto.setApplyOrgId("123");
        dto.setConfirmTime(new Date());
        dto.setCreateTime(new Date());
        dto.setTotalQuantity("234");
        dto.setTotalAmount("234");
        dto.setShipWareHouseId("23");
        dto.setShipQuantity("32");
        dto.setShipMethod(1);
        dto.setShipAreaId("32");
        dto.setSendBackRequired(true);
//        dto.setScItemIds();
        dto.setSapRefPoNo("客户采购参考号");
        dto.setSapCostCenterId("总部成本中心");
        dto.setSapChannel("sap渠道");
        dto.setReturnOrder(false);
        dto.setRemark("备注");
        dto.setReceiveQuantity("32");
        dto.setReceiveWareHouseId("23");
        dto.setReceiveAreaId("23");
        dto.setProductGroup("产品组");
        dto.setOutAllocateApplyOrderCode("sap外部订单号");
        dto.setType(2);
        dto.setFinishTime(new Date());
        dto.setExpectedReceiveTime(new Date());
        dto.setDiscount("23");
        dto.setCustomer("客户");
        dto.setCreator("创建者");

        System.out.println(JSONObject.toJSONString(dto));
    }
}
